//
//  PersonalViewModel.m
//  ChatForHot
//
//  Created by wr on 2018/10/9.
//  Copyright © 2018年 FLY. All rights reserved.
//

#import "PersonalViewModel.h"
#import "PersonalCellModel.h"
#import <AFNetworking/AFNetworking.h>

@interface PersonalViewModel ()

@property (nonatomic, strong, readwrite) NSMutableArray <PersonalCellModel *> *cellDataSource;
@property (nonatomic, strong, readwrite) RACSubject *profileDidSelectSubject;
@property (nonatomic, strong, readwrite) RACSubject *albumDidSelectSubject;
@property (nonatomic, strong, readwrite) RACSubject *photoVaultDidSelectSubject;
@property (nonatomic, strong, readwrite) RACSubject *identificationDidSelectSubject;
@property (nonatomic, strong, readwrite) RACSubject *avatarDidChangeSubject;
@property (nonatomic, assign) NSInteger tipsNum;
@end

@implementation PersonalViewModel

- (void)HOTCHATdidInitialized {
    [super HOTCHATdidInitialized];
   
    self.profileDidSelectSubject = [[RACSubject alloc] init];
    self.albumDidSelectSubject = [[RACSubject alloc] init];
    self.photoVaultDidSelectSubject = [[RACSubject alloc] init];
    self.identificationDidSelectSubject = [[RACSubject alloc] init];
    
    self.avatarDidChangeSubject = [RACSubject subject];
}

- (void)HOTCHATuploadActionImg:(UIImage *)img{
    NSMutableDictionary *mparameters = [[NSMutableDictionary alloc] init];
    [mparameters setValue:[APPINFO defaultManager].accountid forKey:@"accountid"];
   
    [NetWorkTool HOTCHATUploadImageData:UPLOADHEADPIC_URL parameters:mparameters fileData:^(id<AFMultipartFormData>  _Nonnull formData) {
        [formData appendPartWithFileData:img.HOTCHATcompressQualityData name:@"uploadfile" fileName:@"headpic.jpg" mimeType:@"image/jpeg"];
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull HOTCHATtask, NSString * _Nonnull HOTCHATmsgCode, NSDictionary * _Nonnull HOTCHATresultDic) {
        if ([HOTCHATmsgCode isEqualToString:@"0"]) {
            NSString *dataurl = [HOTCHATresultDic valueForKey:@"data"];
            if (dataurl.length > 0) {
                NSString *HOTCHATfullURL = [NSString stringWithFormat:@"%@%@",APIHEADER,dataurl];
                YYImageCache *cache = [YYImageCache sharedCache];
                [cache setImage:img forKey:HOTCHATfullURL];
            }
            [NetWorkTool HOTCHATUpdateToken];
            [APPINFO defaultManager].headPic = dataurl;
            [self.avatarDidChangeSubject sendNext:nil];
        }
        if ([HOTCHATmsgCode isEqualToString:@"1"]) {
            NSString *message = [HOTCHATresultDic valueForKey:@"message"];
            if (message.length > 0) {
                [NetWorkTool HOTCHATshowMessage:message];
            }
        }
        
    } failure:^(NSURLSessionDataTask * _Nonnull HOTCHATtask, NSError * _Nonnull error) {
        
    }];
}

-(NSMutableArray<PersonalCellModel *> *)cellDataSource {
    @weakify(self)
    NSMutableArray *cellDataSource = [[NSMutableArray alloc] init];
    PersonalCellModel *profile = [[PersonalCellModel alloc] init];
    profile.title = NSLocalizedString(@"person6", nil);
    profile.icon = @"personal_profile";
    profile.info = @"";
    profile.tag = 0;
    profile.cellHeight = 50;
    profile.thinLineIsHidden = YES;
    profile.thickLineIsHidden = YES;
    profile.handler = ^{
        @strongify(self)
        [self.profileDidSelectSubject sendNext:nil];
    };
    
    PersonalCellModel *album = [[PersonalCellModel alloc] init];
    album.title = NSLocalizedString(@"person7", nil);
    album.icon = @"personal_album";
    album.info = @"";
    album.tag = 1;
    album.cellHeight = 50;
    album.thinLineIsHidden = YES;
    album.thickLineIsHidden = YES;
    album.handler = ^{
        @strongify(self)
        [self.albumDidSelectSubject sendNext:nil];
    };
    
    PersonalCellModel *photoValut = [[PersonalCellModel alloc] init];
    photoValut.title = NSLocalizedString(@"person8", nil);
    photoValut.icon = @"personal_photovault";
    photoValut.info = @"";
    photoValut.tag = 2;
    photoValut.cellHeight = 50;
    photoValut.thinLineIsHidden = YES;
    photoValut.thickLineIsHidden = YES;
    photoValut.handler = ^{
        @strongify(self)
        [self.photoVaultDidSelectSubject sendNext:nil];
    };
    
    PersonalCellModel *identification = [[PersonalCellModel alloc] init];
    identification.title = NSLocalizedString(@"person9", nil);
    identification.icon = @"personal_identification";
    identification.info = @"";
    identification.tag = 3;
    identification.cellHeight = 44;
    identification.thinLineIsHidden = YES;
    identification.thickLineIsHidden = YES;
    identification.handler = ^{
        @strongify(self)
        [self.identificationDidSelectSubject sendNext:nil];
    };
    
    [cellDataSource addObjectsFromArray:@[profile,album,photoValut,identification]];
    return cellDataSource;
}
@end
